package com.ssm.controller;

import com.ssm.domain.Role;
import com.ssm.domain.UserInfo;
import com.ssm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author: my_code
 * @Description: 用户管理
 * @Date: Create in 1:27 2020/8/24
 */

@Controller
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private IUserService userService;


    /*
     * @param : []
     * @return : org.springframework.web.servlet.ModelAndView
     * @Description :
     */
    @RequestMapping("/findAll.do")
    //只有Admin用户可以执行该方法
    /* @PreAuthorize("authentication.principal.username == 'admin'")*/
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<UserInfo> userList = userService.findAll();
        mv.addObject("userList", userList);
        mv.setViewName("user-list");
        return mv;
    }

    /*
     * @param : [userId]
     * @return : org.springframework.web.servlet.ModelAndView
     * @Description :
     */
    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name = "id") String userId) throws Exception {
        ModelAndView mv = new ModelAndView();
        UserInfo userInfo = userService.findByUserId(userId);
        mv.addObject("user", userInfo);
        mv.setViewName("user-show");
        return mv;
    }


    @RequestMapping("/save.do")
    /*
     * @param : [user]
     * @return : java.lang.String
     * @Description :
     */
    public String save(UserInfo user) throws Exception {
        userService.save(user);
        return "redirect:findAll.do";
    }



    @RequestMapping("/deleteById.do")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    /*
     * @param : [userId]
     * @return : java.lang.String
     * @Description :
     */
    public String deleteById(@RequestParam(name = "id") String userId) throws Exception {
        userService.deleteById(userId);
        return "redirect:findAll.do";
    }



    /*
     * @return : org.springframework.web.servlet.ModelAndView
     * @Description :
     * @param : [userId]
     */
    @RequestMapping("/findRoleByUserId.do")
    public ModelAndView findRoleByUserId(@RequestParam(name = "id") String userId) throws Exception {
        ModelAndView mv = new ModelAndView();
        UserInfo userInfo = userService.findByUserId(userId);
        List<Role> otherRoles = userService.findOtherRoles(userId);
        mv.addObject("user", userInfo);
        mv.addObject("otherRoles", otherRoles);
        mv.setViewName("user-role-add");
        return mv;
    }



    /*
     * @return : java.lang.String
     * @Description :
     * @param : [userId, roleIds]
     */
    @RequestMapping("/addRoleToUser.do")
    public String addRoleToUser(String userId, @RequestParam(name = "ids") String[] roleIds) throws Exception {
        userService.addRoleToUser(userId, roleIds);
        return "redirect:findRoleByUserId.do?id=" + userId;
    }
}
